package com.noah.project.controller

import com.noah.project.dao.AboutDao
import com.noah.project.dao.CareerDao
import com.noah.project.dao.CompanyDao
import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.global.TokenManager
import com.noah.project.model.About
import com.noah.project.model.AliasAbout
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class AboutApiController {
    @Autowired
    lateinit var aboutDao: AboutDao
    @Autowired
    lateinit var careerDao: CareerDao
    @Autowired
    lateinit var companyDao: CompanyDao
    val log = LoggerFactory.getLogger(AboutApiController::class.simpleName)
    /*****************日记******************/
    @RequestMapping(Const.API_ADD_ABOUT)
    fun addAbout(about: About, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retAbout = aboutDao.findByAlias(about.alias)
            if (retAbout == null) {
                about.createTime = System.currentTimeMillis()
                val retAid = aboutDao.insert(about)
                if (retAid > 0) {
                    return Reply(code = Const.SUCCESS, msg = "创建成功", data = JsonManager.toJson(about))
                }
            } else {
                return Reply(code = Const.ERROR_ALREADY, msg = "地址别名已经存在")
            }
            return Reply(code = Const.FAIL, msg = "创建失败")
        } catch (e: Exception) {
            log.error("创建简介", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }

    @RequestMapping(Const.API_DELETE_ABOUT)
    fun deleteAbout(aid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = aboutDao.delete(aid)
            if (ret > 0) {
                return Reply(code = Const.SUCCESS, msg = "删除成功")
            }
            return Reply(code = Const.FAIL, msg = "删除失败")
        } catch (e: Exception) {
            log.error("删除简介", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_ABOUT)
    fun updateAbout(about: About, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retAid = aboutDao.update(about)
            if (retAid > 0) {
                val retAbout = aboutDao.findById(about.aid)
                return Reply(code = Const.SUCCESS, msg = "修改成功", data = JsonManager.toJson(retAbout))
            }
            return Reply(code = Const.FAIL, msg = "修改失败")
        } catch (e: Exception) {
            log.error("更新简介", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }

    @RequestMapping(Const.API_GET_ABOUT_LIST)
    fun getAboutList(uid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retNotes = aboutDao.findAbouts(uid)
            if (retNotes.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取简介列表成功", data = JsonManager.toJson(retNotes))
            }
            return Reply(code = Const.FAIL, msg = "没有更多简介")
        } catch (e: Exception) {
            log.error("简介列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }

    @RequestMapping(Const.API_GET_ABOUT)
    fun getAbout(aid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retAbout = aboutDao.findById(aid)
            if (retAbout != null) {
                return Reply(code = Const.SUCCESS, msg = "获取简介成功", data = JsonManager.toJson(retAbout))
            }
            return Reply(code = Const.FAIL, msg = "找不到该简介")
        } catch (e: Exception) {
            log.error("单个简介", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }

    @RequestMapping(Const.API_GET_ABOUT_BY_ALIAS)
    fun getAboutByAlias(a: String?): Reply {
        try {
            val retAbout = aboutDao.findByAlias(a)
            if (retAbout != null) {
                val companies = companyDao.findCompanies(retAbout.aid)
                val careers = careerDao.findCareers(retAbout.aid)
                val aliasAbout = AliasAbout(retAbout, companies, careers)
                return Reply(code = Const.SUCCESS, msg = "获取简介成功", data = JsonManager.toJson(aliasAbout))
            }
            return Reply(code = Const.FAIL, msg = "找不到该简介")
        } catch (e: Exception) {
            log.error("单个简介web", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "简介异常")
        }
    }
}